library(foreign)
library(lm.beta)
library(sjPlot)
library(ggplot2)
library(sjlabelled)
library(jtools)

####Datasets
data1 = read.spss("C:\\Users\\34626\\Dropbox\\MiLab\\R&R\\Political Anger ZC_HMM_HGZ\\R&R 3\\rep1.sav", use.value.labels = T, to.data.frame=TRUE)
data2 = read.spss("C:\\Users\\34626\\Dropbox\\MiLab\\R&R\\Political Anger ZC_HMM_HGZ\\R&R 3\\rep2.sav", use.value.labels = T, to.data.frame=TRUE)

####Table 1
cross <- lm(anger1 ~ sex + age + edu + income + white + polid1 + PolInt + netsize1 + onpoldi1 + socnews1 + tranews1 + homo1, data=data1)
summary(cross)
test.beta <- lm.beta(cross)
coef(test.beta, standardized=T)

lagged <- lm(anger2 ~ sex + age + edu + income + white + polid1 + PolInt + netsize1 + onpoldi1 + socnews1 + tranews1 + homo1, data=data2)
summary(lagged)
test.beta <- lm.beta(lagged)
coef(test.beta, standardized=T)

auto <- lm(anger2 ~ anger1 + sex + age + edu + income + white + polid1 + PolInt + netsize1 + onpoldi1 + socnews1 + tranews1 + homo1, data=data2)
summary(auto)
test.beta <- lm.beta(auto)
coef(test.beta, standardized=T)

####Table 2
test1 <- lm(anger1 ~ sex + age + edu + income + white + polid1 + PolInt + netsize1 + onpoldi1 + socnews1 + tranews1 + homo1 + socnews1*homo1, data=data1)
summary(test1)
test.beta <- lm.beta(test1)
coef(test1)

test2 <- lm(anger2 ~ sex + age + edu + income + white + polid1 + PolInt + netsize1 + onpoldi1 + socnews1 + tranews1 + homo1 + socnews1*homo1, data=data2)
summary(test2)
test.beta <- lm.beta(test1)
coef(test2)

test3 <- lm(anger2 ~ anger1 + sex + age + edu + income + white + polid1 + PolInt + netsize1 + onpoldi1 + socnews1 + tranews1 + homo1 + socnews1*homo1, data=data2)
summary(test3)
test.beta <- lm.beta(test3)
coef(test3)

####Figure 2
plot_summs(test, lagged, auto,  plot.distributions = F,
           coefs=c("Social media news" = "socnews1",
                   "Political homophily" = "homo1"), 
           model.names = c("Cross-sectional", "Lagged", "Autoregressive"),
           scale= T, transform.response = TRUE, colors=c("CUD"))


####Figure 3
plot_model(test1, type = "int", terms = c("homo1","socnews1"), mdrt.values = c("minmax"), axis.title = c("Social Media News", "Political Anger"), legend.title = c("Social Media Homophily"), title = "", colors=c( "olivedrab3", "steelblue"))
plot_model(test2, type = "int", terms = c("homo1","socnews1"), mdrt.values = c("minmax"), axis.title = c("Social Media News", "Political Anger"), legend.title = c("Social Media Homophily"), title = "", colors=c( "olivedrab3", "steelblue"))
plot_model(test3, type = "int", terms = c("homo1","socnews1"), mdrt.values = c("minmax"), axis.title = c("Social Media News", "Political Anger"), legend.title = c("Social Media Homophily"), title = "", colors=c( "olivedrab3", "steelblue"))

